﻿using SearchAlgorithms.Services;
using static SearchAlgorithms.Services.ConsoleBlock;

namespace SearchAlgorithmsConsole
{
    public class Program
    {
        static async Task Main(string[] args)
        {
            string filePathA = "inputa.bin";
            string filePathB = "inputf.bin";

            int bufferSize = 15;
            byte[] pattern = { 0x01, 0x02, 0x03, 0x04 };
            string SearchMethod = "IndexOf";
            string CompareMethod = "Comparison";

            List<string> message = new List<string>();

            FileReadBlock fileReadBlock1 = new FileReadBlock(filePathA, bufferSize);
            FileReadBlock fileReadBlock2 = new FileReadBlock(filePathB, bufferSize);
            
            SegmentBlock segmentBlock1 = new SegmentBlock(pattern, bufferSize, SearchMethod);
            SegmentBlock segmentBlock2 = new SegmentBlock(pattern, bufferSize, SearchMethod);
            
            // 整合模块
            IntegrateBlock integrateBlock1 = new IntegrateBlock();
            IntegrateBlock integrateBlock2 = new IntegrateBlock();

            CompareBlock compareBlock = new CompareBlock(CompareMethod);
            ConsoleResultBlock consoleResultBlock = new ConsoleResultBlock();

            fileReadBlock1.DataArrived += (data) =>
            {
                segmentBlock1.Enqueue(data);
            };

            fileReadBlock2.DataArrived += (data) =>
            {
                segmentBlock2.Enqueue(data);
            };

            segmentBlock1.DataArrived += (data) =>
            {
                integrateBlock1.Enqueue(data);
            };
            segmentBlock2.DataArrived += (data) =>
            {
                integrateBlock2.Enqueue(data);
            };

            integrateBlock1.DataArrived += (data) =>
            {
                compareBlock.Enqueue_1(data);
            };
            integrateBlock2.DataArrived += (data) =>
            {
                compareBlock.Enqueue_2(data);
            };

            compareBlock.DataArrived += (data) =>
            {
                consoleResultBlock.Enqueue(data);
            };

            consoleResultBlock.DataArrived += (data) =>
            {
                foreach (var word in data)
                {
                    message.Add(word);
                }
                // 停止所有模块
                segmentBlock1._Process.Complete();
                segmentBlock2._Process.Complete();
                integrateBlock1._Process.Complete();
                integrateBlock2._Process.Complete();
                compareBlock.compareBlock.Complete();
                consoleResultBlock._InputBlock.Complete();
            };

            Task readTask1 = Task.Run(() => fileReadBlock1.ReadBytes());
            Task readTask2 = Task.Run(() => fileReadBlock2.ReadBytes());

            // 确保所有任务都已完成
            readTask1.Wait(); // 等待文件读取任务完成
            readTask2.Wait();

            // 等待控制台输出块和分段块完成
            // 确保所有任务都已完成
            await Task.WhenAll(
                segmentBlock1._Process.Completion, segmentBlock2._Process.Completion, integrateBlock1._Process.Completion, integrateBlock2._Process.Completion, compareBlock.compareBlock.Completion,
                consoleResultBlock._InputBlock.Completion
            );

            
            if (message.Count > 0)
            {
                foreach (var word in message)
                {
                    Console.WriteLine(word);
                }
                Console.WriteLine($"文件不同,共有{message.Count}帧不同。");
            }
            else
            {
                Console.WriteLine("文件相同");
            }


            Console.WriteLine("所有Block处理完成，主程序结束。");


        }
    }
}
